package com.smart.mip.system.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class SecurityUser extends UserInfo implements UserDetails {
	
	private static final long serialVersionUID = 1L;
	
	

	public SecurityUser(UserInfo userInfo) {
		if (userInfo != null) {
			this.setUserCode(userInfo.getUserCode());
			this.setUserPin(userInfo.getUserPin());
			this.setLoginName(userInfo.getLoginName());
			this.setUserName(userInfo.getUserName());
			this.setUserType(userInfo.getUserType());
			this.setIsValid(userInfo.getIsValid());
			this.setUserRoles(userInfo.getUserRoles());
		}
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> authorities = new ArrayList<>();
		List<UserRole> userRoles = this.getUserRoles();
		if (!userRoles.isEmpty()) {
			for (UserRole role : userRoles) {
				SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getRoleCode());
				authorities.add(authority);
			}
		}
		return authorities;
	}

	@Override
	public String getPassword() {
		return super.getUserPin();
	}

	@Override
	public String getUsername() {
		return super.getLoginName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

}
